package com.zby.business.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.zby.business.R;

/**
 * Created by BY on 2016/1/26.
 */
public class LoadingImageView extends ImageView {
    private int left, right, top, bottom;
    private float width, height, textWidth;
    private float startAngle = 0, stopAngle = 30;
    private int lineWidth, lineColor;
    private Paint paint, textPaint;
    private String remindText;
    private boolean flag = true;
    private ImageThread thread;
    private float speed, baseSpeed;
    private float sweepAngle;

    public LoadingImageView(Context context) {
        super(context);
    }

    public LoadingImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LoadingImage);
        lineWidth = ta.getInteger(R.styleable.LoadingImage_lineWidth, 10);
        width = ta.getDimension(R.styleable.LoadingImage_imageWidth, 150);
        height = ta.getDimension(R.styleable.LoadingImage_imageHeight, 150);
        remindText = ta.getString(R.styleable.LoadingImage_remindText);
        speed = ta.getInteger(R.styleable.LoadingImage_speed, 9);
        sweepAngle = ta.getInteger(R.styleable.LoadingImage_sweepAngle, 300);
        baseSpeed = ta.getInteger(R.styleable.LoadingImage_baseSpeed, 5);
        lineColor = ta.getInteger(R.styleable.LoadingImage_lineColor, Color.WHITE);
        paint = new Paint();
        textPaint = new Paint();
        paint.setColor(lineColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(lineWidth);
        paint.setStyle(Paint.Style.STROKE);
        left = (int) (width / 5);
        top = (int) (left - width / 10);
        right = (int) (width - left);
        bottom = (int) (right - width / 10);
        textPaint.setAntiAlias(true);
        textPaint.setColor(lineColor);
        textPaint.setTextSize(37);
        textWidth = textPaint.measureText(remindText);
        ta.recycle();
    }

    public LoadingImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.parseColor("#262525"));
        canvas.drawArc(new RectF(left, top, right, bottom), startAngle, stopAngle - startAngle, false, paint);
        canvas.drawText(remindText, (getWidth() - textWidth) / 2, (getHeight() - bottom) / 2 + bottom + 45, textPaint);
        if (thread == null) {
            thread = new ImageThread();
            thread.start();
        }
    }

    class ImageThread extends Thread {


        @Override
        public void run() {
            while (true) {
                if (stopAngle - startAngle < sweepAngle) {
                    if (stopAngle - startAngle < 30) {
                        startAngle = startAngle + baseSpeed;
                        stopAngle = stopAngle + baseSpeed + speed;
                        flag = true;
                    } else {
                        if (flag) {
                            startAngle = startAngle + baseSpeed;
                            stopAngle = stopAngle + baseSpeed + speed;
                        } else {
                            startAngle = startAngle + baseSpeed + speed;
                            stopAngle = stopAngle + baseSpeed;
                        }
                    }
                } else {
                    startAngle = startAngle + baseSpeed + speed;
                    stopAngle = stopAngle + baseSpeed;
                    flag = false;
                }
                postInvalidate();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
